package cn.cloudplug.ui.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatDialog;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

import java.io.File;
import java.util.List;

/**
 * Created by pc on 2018/1/24.
 */

public class PicPagerAdapter extends PagerAdapter {
    public static final String TAG = "PicPagerAdapter";
    private static final int KEY = 0x6666;
    private List<File> files;
    private android.content.Context context;
    private AppCompatDialog dialog;

    public PicPagerAdapter(Context context,AppCompatDialog dialog,List<File> files) {

        this.context=context;
        this.dialog=dialog;
        this.files = files;
    }

    @Override
    public int getCount() {

        Log.e(TAG,"getCount");
        return files==null?0:files.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        Log.e(TAG,"instantiateItem");
        SubsamplingScaleImageView imageView=new SubsamplingScaleImageView(context);
        container.addView(imageView);
        imageView.setImage(ImageSource.uri(files.get(position).getAbsolutePath()));
        imageView.setTag(position);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        Log.e(TAG,"destroyItem");
        if (position == 0 && files.size() == 0) {
            dialog.dismiss();
            return;
        }
        if (position == files.size()) {
            container.removeView((View) object);
        } else {
            container.removeView((View) object);
        }
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        Log.e(TAG,"isViewFromObject");
        return view==object;
    }
    @Override
    public int getItemPosition(Object object) {
        Log.e(TAG,"getItemPosition");
        return (int) ((View)object).getTag();
    }
}
